package com.woniuxy.yoga.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.pojo.*;
import com.woniuxy.yoga.pojo.Coach;
import com.woniuxy.yoga.pojo.CoachVo;
import com.woniuxy.yoga.pojo.Courseuser;
import com.woniuxy.yoga.service.CoachService;
import com.woniuxy.yoga.service.CourseuserService;
import com.woniuxy.yoga.service.OrdersService;
import com.woniuxy.yoga.service.*;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
<<<<<<< HEAD
 * @author Y_Chi
 * @since 2020-10-23
=======
 * @author axq
 * @since 2020-10-22
>>>>>>> yoga0.0.3 +friends
 */
@RestController
@RequestMapping("/yoga/coach")
public class CoachController {


    @Autowired
    private CoachService coachService;
    @Autowired
    private CourseuserService courseuserService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UsersService usersService;


    @ApiOperation(value = "分页查询教练")
    @GetMapping(value = "/coachByPage")
    @CrossOrigin
    public R getCoachByPage(@ApiParam(value = "当前页")@RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
                            @ApiParam(value = "每页的记录数")@RequestParam(value = "limit",required = true,defaultValue = "6") Integer limit,
                            CoachVo coachVo){
        //创建Page对象
        Page pageBean = new Page<>(page,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(coachVo.getCname())){
            queryWrapper.like("cname",coachVo.getCname());
        }
        if(coachVo.getFreetime()!=null){
            queryWrapper.eq("freetime",coachVo.getFreetime());
        }

        if(coachVo.getLevelid()!=null){
            queryWrapper.eq("levelid",coachVo.getLevelid());
        }
        Page resultPage = coachService.page(pageBean, queryWrapper);

        //获取集合
        List records = resultPage.getRecords();

        //获取总记录数
        long total = resultPage.getTotal();
        if(records!=null){
            return R.ok().data("records",records).data("total",total);
        }else{
            return R.error().message("没有查到集合数据");
        }

    }

    @CrossOrigin
    @ApiOperation("查询(登录本人)(身份教练)个人信息")
    @GetMapping(value = "my/{accountid}")
    public R getMyself(@PathVariable Integer accountid){
        System.out.println(accountid);
        QueryWrapper query=new QueryWrapper();
        query.eq("accountid",accountid);
        Coach one    = coachService.getOne(query);
        return R.ok().data("mycoach",one).message("查询成功");
    }

    @CrossOrigin
    @ApiOperation("查询教练我的订单")
    @GetMapping(value = "myorder")
    public R getMyOrder(@ApiParam("当前页") @RequestParam(value="page",defaultValue = "1") Integer page,
                        @ApiParam("每页的记录数")@RequestParam(value = "limit",defaultValue = "5") Integer limit,OrderVo orderVo){
        Page pageBean=new Page<>(page,limit);
        QueryWrapper query=new QueryWrapper();
        query.eq("payee",orderVo.getPayee());


        if(orderVo.getPayer()!=null&&!orderVo.getPayer().equals("")){
            query.eq("payer",orderVo.getPayer());
        }
        if(orderVo.getStatus()!=null&&!orderVo.getStatus().equals("")){
            query.eq("status",orderVo.getStatus());
        }
        Page result=ordersService.page(pageBean,query);
        List list = result.getRecords();
        Long total=result.getTotal();
        if(total!=null) {
            return R.ok().data("payee", list).data("total",total);
        }else{
             return  R.error().message("查询错误");
        }

    }
    @CrossOrigin
    @ApiOperation("查看我的信息")
    @GetMapping(value = "msg")
    public R getMsg(@ApiParam("当前页")@RequestParam(value = "page",defaultValue = "1") Integer page,
                    @ApiParam("每页记录数")@RequestParam(value = "limit",defaultValue = "5") Integer limit, Integer fromid){
        Page pageBean=new Page<>(page,limit);
        QueryWrapper query=new QueryWrapper();
        query.eq("fromid",fromid);
        Page result=messageService.page(pageBean,query);
        List list = result.getRecords();
        Long total=result.getTotal();
        if(total!=null) {
            return R.ok().data("msg", list).data("total",total);
        }else {
            return R.error().message("查看我的信息出现错误");
        }
    }

    @CrossOrigin
    @ApiOperation("确认状态")
    @GetMapping(value = "status/{ordered}")
    public R upStatus(@PathVariable("ordered") Integer ordered) {
        Orders orders = new Orders();
        orders = ordersService.getById(ordered);
        if (orders.getStatus() == 0) {
            orders.setStatus(2);
            ordersService.updateById(orders);
            return R.ok();
        } else {
            return R.error();
        }
    }
    @CrossOrigin
    @ApiOperation("拒绝状态")
    @GetMapping(value = "status2/{ordered}")
    public R upStatus2(@PathVariable("ordered") Integer ordered) {
        Orders orders = new Orders();
        orders = ordersService.getById(ordered);
        if (orders.getStatus() == 0) {
            orders.setStatus(4);
            ordersService.updateById(orders);
            return R.ok();
        } else {
            return R.error();
        }
    }

    @CrossOrigin
    @ApiOperation("查看我的消息")
    @GetMapping(value = "msg/{fromid}")
    public R getMsg(@PathVariable("fromid") Integer fromid){
        QueryWrapper query=new QueryWrapper();
        query.eq("fromid",fromid);
        List<Message> list = messageService.list(query);
        return R.ok().data("msg",list);
    }

    @CrossOrigin
    @ApiOperation("信息确认状态")
    @GetMapping(value = "mstatus/{messageid}")
    public R statusUp(@PathVariable("messageid") Integer messageid) {
        Message msg = new Message();
        msg = messageService.getById(messageid);
        if (msg.getStatus() == 2) {
            msg.setStatus(3);
            messageService.updateById(msg);
            return R.ok();
        } else {
            return R.error();
        }
    }
    @CrossOrigin
    @ApiOperation("信息拒绝状态")
    @GetMapping(value = "mstatus2/{messageid}")
    public R statusUp2(@PathVariable("messageid") Integer messageid) {
        Message msg = new Message();
        msg = messageService.getById(messageid);
        if (msg.getStatus() == 2) {
            msg.setStatus(1);
            messageService.updateById(msg);
            return R.ok();
        } else {
            return R.error();
        }
    }
    @CrossOrigin
    @ApiOperation("我的好友")
    @GetMapping(value="friend/{accountid}")
    public R getFriend(@PathVariable Integer accountid){
        List<Users> friend=usersService.getFriend(accountid);
        return R.ok().data("friend",friend);

    }
    @CrossOrigin
    @ApiOperation("查询教练所属学员")
    @GetMapping(value = "student/{accountid}")
    public R getStu(@PathVariable Integer accountid){
        Integer coachid = coachService.getOne((new QueryWrapper<Coach>()).eq("accountid",accountid)).getCoachid();
        List<Users> user=usersService.getStu1(coachid);
        return R.ok().data("student",user);
    }


    @ApiOperation("修改个人信息")
    @PutMapping("/update")
    @CrossOrigin
    public R updateMessage(@RequestBody Coach form){
        QueryWrapper<Coach> wrapper = new QueryWrapper<>();
        if(form!=null){
            wrapper.eq("accountid",form.getAccountid());
            Coach coach = coachService.getOne(wrapper);
            form.setCoachid(coach.getCoachid());
            System.out.println(form.getCoachid());
            boolean b = coachService.updateById(form);
            if (b){
                return R.ok().message("修改成功");
            }else {
                return R.error().message("修改失败");
            }
        }
        return R.error().message("修改失败");

    }

    @ApiOperation(value = "得到单个教练")
    @GetMapping("/getCaochById/{id}")
    @CrossOrigin
    public R getClubById(@PathVariable("id")Integer id){
        System.out.println(id);
        Coach coach = coachService.getById(id);
        if (coach != null) {
            return R.ok().data("coach", coach);
        } else {
            return R.error().message("无法查询俱乐部");
        }
    }
}

